package com.deimos.tests;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.llom.util.AXIOMUtil;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.vast.ows.sps.CancelResponse;
import org.vast.ows.sps.SPSUtils;
import org.vast.ows.sps.StatusReport;
import org.vast.ows.sps.StatusReport.RequestStatus;
import org.vast.xml.DOMHelper;

import com.deimos.HMA;
import com.deimos.UtilsDeimos;
import com.deimos.dataBase.TaskController;
import org.vast.ows.OWSException;

public class Cancel {
    
    private static HMA hma = new HMA();
    static SPSUtils utils = new SPSUtils();
    static String taskId = null;
    
    @BeforeClass
    public static void init() {
	TaskController taskController = new TaskController();
	taskController.createTables(false);
    }
    
    @Before
    public void cancel_Step1() throws Exception {
	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "Cancel_Syn_Feasible_Step_1_Submit_Request.xml"));
	response = hma.Submit(request);
	
	StatusReport report = UtilsDeimos.readParams(response.toString()).getReport();
	if (report.getDescription().contains(" is in use by the task ")){
	    taskId = report.getDescription().split(" is in use by the task ")[1];  
	    cancel_Step3();
	    taskId = null;
	    cancel_Step1();
	}
	
	if (taskId == null) taskId = UtilsDeimos.readParams(response.toString()).getReport().getTaskID();
	assertTrue(taskId != null);
    }

    
    @Test
    public void cancel_Syn_Feasible() throws Exception {
	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "Cancel_Syn_Feasible_Step_2_Cancel_Request.xml").replace("TASKID", taskId));
	response = hma.Cancel(request);
	InputStream is = new ByteArrayInputStream(response.toString().getBytes());
	DOMHelper domResponse = new DOMHelper(is, false);
	RequestStatus status = ((CancelResponse) utils.readXMLResponse(domResponse, domResponse.getRootElement(), "SPS", "CancelResponse", "2.0")).getReport().getRequestStatus();
	assertTrue(status == RequestStatus.Accepted);
    }
    
    
    @Test
    public void cancel_Incorrect_Task_Status() throws Exception{
	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "Cancel_Syn_Feasible_Step_3_Cancel_Request.xml").replace("TASKID", taskId));
	response = hma.Cancel(request);
	InputStream is = new ByteArrayInputStream(response.toString().getBytes());
	DOMHelper domResponse = new DOMHelper(is, false);
	RequestStatus status = ((CancelResponse) utils.readXMLResponse(domResponse, domResponse.getRootElement(), "SPS", "CancelResponse", "2.0")).getReport().getRequestStatus();
	
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "Cancel_Syn_Feasible_Step_3_Cancel_Request.xml").replace("TASKID", taskId));
	response = hma.Cancel(request);
	is = new ByteArrayInputStream(response.toString().getBytes());
	domResponse = new DOMHelper(is, false);
	status = ((CancelResponse) utils.readXMLResponse(domResponse, domResponse.getRootElement(), "SPS", "CancelResponse", "2.0")).getReport().getRequestStatus();
	assertTrue(status == RequestStatus.Rejected);
    }
    
    @Test
    public void cancel_Task_Not_Found() throws Exception {
	try{
	OMElement request;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "Cancel_Task_Not_Found_Request.xml"));
	hma.Cancel(request);
	fail();
	} catch(OWSException ex){
	    assertTrue(ex.getMessage().compareTo("The information was not found in the data base") == 0);
	}
    }
    
    public static void cancel_Step3() throws Exception{
	OMElement request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "Cancel_Syn_Feasible_Step_3_Cancel_Request.xml").replace("TASKID", taskId));
	hma.Cancel(request);
    }
}
